package com.qingzhuge.manager.controller;

import com.google.gson.Gson;
import com.qingzhuge.common.annotation.ASysLog;
import com.qingzhuge.common.enums.EAuthorityType;
import com.qingzhuge.common.enums.EResourceType;
import com.qingzhuge.common.validator.ValidatorUtil;
import com.qingzhuge.controller.BasePkLongController;
import com.qingzhuge.core.annotation.AResource;
import com.qingzhuge.dto.response.ResponseBodyDto;
import com.qingzhuge.file.FileStorageEnum;
import com.qingzhuge.file.dto.request.SysFileRequestPkStringDto;
import com.qingzhuge.file.dto.response.SysFileResponsePkStringDto;
import com.qingzhuge.file.entity.FileStorageConfig;
import com.qingzhuge.file.entity.SysFile;
import com.qingzhuge.file.service.ISysFileService;
import com.qingzhuge.file.service.cloud.FileStorageFactory;
import com.qingzhuge.file.validator.group.AliyunGroup;
import com.qingzhuge.file.validator.group.QiniuGroup;
import com.qingzhuge.sys.entity.SysConfig;
import com.qingzhuge.sys.service.ISysConfigService;
import com.qingzhuge.utils.PageUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import static com.qingzhuge.file.entity.FileStorageConfig.CLOUD_STORAGE_CONFIG_KEY;

/**
 * @author : zero.xiao
 * @description : 文件管理
 * @date :2018/6/8 0008 下午 14:46
 * @modified :
 */
@ASysLog(value = "文件管理")
@Api(tags = "S文件管理")
@AResource(name = "文件管理", type = EResourceType.menu, catalog = "系统管理")
@RestController
@RequestMapping("api/sys/auth/file")
public class SysFileController extends BasePkLongController<
        SysFile, ISysFileService, SysFileRequestPkStringDto, SysFileResponsePkStringDto> {
    private final ISysConfigService sysConfigService;

    private final static String KEY = CLOUD_STORAGE_CONFIG_KEY;

    @Autowired
    public SysFileController(ISysConfigService sysConfigService) {
        this.sysConfigService = sysConfigService;
    }

    @Override
    public ResponseEntity<ResponseBodyDto<PageUtil<SysFileResponsePkStringDto>>> queryPage(@RequestBody(required = false) SysFileRequestPkStringDto param) {
        return super.queryPage(param);
    }

    @ApiOperation(value = "配置详情")
    @AResource(name = "配置详情", type = EResourceType.button, authority = EAuthorityType.DETAIL)
    @GetMapping("config")
    @PreAuthorize("hasRole('ROOT') or hasAnyAuthority('SYSTEM','ADMIN') and hasAuthority('DETAIL')")
    public ResponseEntity<ResponseBodyDto<FileStorageConfig>> config() {
        FileStorageConfig config = sysConfigService.getConfigObject(KEY, FileStorageConfig.class);
        return returnSuccess(config);
    }


    @ApiOperation(value = "保存配置")
    @AResource(name = "保存配置", type = EResourceType.button, authority = EAuthorityType.UPDATE)
    @PutMapping("config")
    @PreAuthorize("hasRole('ROOT') or hasAnyAuthority('SYSTEM','ADMIN') and hasAuthority('UPDATE')")
    public ResponseEntity<ResponseBodyDto<Object>> saveConfig(@RequestBody FileStorageConfig config) {
        //校验类型
        ValidatorUtil.validateEntity(config);
        if (config.getType() == FileStorageEnum.QINIU) {
            //校验七牛数据
            ValidatorUtil.validateEntity(config, QiniuGroup.class);
        } else if (config.getType() == FileStorageEnum.ALIYUN) {
            //校验阿里云数据
            ValidatorUtil.validateEntity(config, AliyunGroup.class);
        }

        SysConfig sysConfig = sysConfigService.getValue(KEY);
        if (null == sysConfig) {
            sysConfig = new SysConfig();
            sysConfig.setKey(KEY);
        }
        sysConfig.setValue(new Gson().toJson(config));
        sysConfigService.save(sysConfig);
        return returnSuccess();
    }

    @Override
    public ResponseEntity<ResponseBodyDto<Object>> delete(@PathVariable Long id) {
        SysFile sysFile = service.findById(id);
        if (null != sysFile && StringUtils.isNotBlank(sysFile.getUrl())) {
            FileStorageFactory.build().delete(sysFile.getUrl());
        }
        return super.delete(id);
    }
}
